/*eslint-disable*/
/**
 * 视频回放功能
 */
class PlaybackOCX {
    constructor(options) {
        this.divId = options.divId;
        this.theme = options.theme;
        this.SUCCESS = 0; /* 控件操作成功 */
        // Token请求通知
        this.PLAYBACK_EVENT_TOKEN = 33554438;
        // 窗口选中改变通知
        this.PLAYBACK_EVENT_WINDOW = 33554449;
        // 回放开始通知
        this.PLAYBACK_EVENT_START = 33554464;
        // 回放停止通知
        this.PLAYBACK_EVENT_STOP = 33554465;
        //下载进度事件
        this.EVENT_DOWNLOAD = 0x08000001;
        this.pictype = "jpeg"; //setGloblePara的xml参数
        this.picpath = "C:\Users\Public\Pictures\\"; //setGloblePara的xml参数
        this.videosize = "256"; //setGloblePara的xml参数
        this.videopath = "C:\Users\Public\Videos\\"; //setGloblePara的xml参数
        this.decode = "5"; //setGloblePara的xml参数
        // 存放回放控件回调函数的数组
        this.callback = [];
        // 回放控件
        this.playbackOCX
        this.tokenCallEvent = (reqID) => {}
        // 将预览事件放置全局
        window.Notify = this.Notify.bind(this)
        window.OnFireNotify = this.OnFireNotify.bind(this)
        this.init()
        this.setCallBack()
        this.setGlobalParam(this.getParmXml(this.pictype, this.picpath, this.videosize, this.videopath, this.decode));
    }
    init() {
        // 获取控件所在DIV
        var divObj = document.getElementById(this.divId);
        //alert("isBrowserIE()="+isBrowserIE());
        if (this.isBrowserIE() == 2) { // IE11
            // 加载控件
            var o = document.createElement("object");
            o.setAttribute("classid", "clsid:9FBB8076-0B0E-49F0-9013-7B74B1C4CABB");
            o.setAttribute("width", "100%");
            o.setAttribute("height", "100%");
            o.setAttribute("id", "PlayBackOCX");
            o.setAttribute("name", "PlayBackOCX");
            divObj.appendChild(o);
            var script = document.createElement("script");
            script.setAttribute("language", "javascript");
            script.setAttribute("for", "PlayBackOCX");
            script.setAttribute("event", "Notify(event_type, wnd_index, lparam, sparam)");
            script.innerHTML = "Notify(event_type, wnd_index, lparam, sparam);";
            divObj.appendChild(script);
        } else {
            if (-[1, ]) { // IE9,IE10,chrome39
                divObj.innerHTML = '<embed id="PlayBackOCX" skin="' + this.theme + '" type="application/npPlatformPlayback-plugin"  name="PlayBackOCX" width="100%" height="100% ip="10.33.30.243"></embed>';
            } else { // IE6,7,8
                var object2 = document.createElement("object");
                divObj.innerHTML = '<object id=\"PlayBackOCX\"Theme=\"' + this.theme + '\"classid=\"clsid:9FBB8076-0B0E-49F0-9013-7B74B1C4CABB\" width=\"100%\" height=\"100%\" ><param name="Theme" value =\"' + this.theme + '\"></object>' +
                    '<script language=\"javascript\" for=\"PlayBackOCX\" event=\"Notify(event_type, wnd_index, lparam, sparam)\">Notify(event_type, wnd_index, lparam, sparam);</script>';
            }
        }
        // 回放控件
        this.playbackOCX = document.getElementById("PlayBackOCX");

    }
    //全局参数xml拼接
    getParmXml(picType, picPath, videoSize, videoPath, decode) {
        var xml = [];
        xml.push("<?xml version=\"1.0\" encoding=\"utf-8\"?><global>");
        xml.push("<snap picture=\"" + picType + "\" save=\"" + picPath + "\"/>");
        xml.push("<clip packSize=\"" + videoSize + "\" save=\"" + videoPath + "\"/>");
        xml.push(" <performance decode=\"" + decode + "\"/>");
        xml.push("</global>");
        return xml.join("");
    }
    // 设置token验证函数
    setTokenCallEvent(fn) {
        this.tokenCallEvent = fn
    }
    // 设置回调函数
    setCallBack() {
        //设置token回调
        this.registerCallback(
            this.PLAYBACK_EVENT_TOKEN,
            this.tokenCallEvent
        );
        this.registerCallback(this.PLAYBACK_EVENT_WINDOW, this.windowChange);
        this.registerCallback(this.PLAYBACK_EVENT_START, this.startPlayback);
        this.registerCallback(this.PLAYBACK_EVENT_STOP, this.stopPlay);
        this.registerCallback(this.EVENT_DOWNLOAD, this.downloadState);

    }
    //注册回调事件开始
    windowChange() {
        console.log("事件通知：窗口选中改变");
    }

    startPlayback() {
        console.log("事件通知：回放开始");
    }

    stopPlay() {
        console.log("事件通知：回放停止");
    }
    downloadState(lparam, wparam) {
        // var percent = wparam.getElementsByTagName("download");
        console.log("句柄为：" + lparam + "下载状态：" + wparam);
    }

    /**
     * 设置全局参数
     * 
     * @param:xml 全局参数
     * @returns:操作发起成功0；失败返回错误码
     */
    setGlobalParam(xml) {
        try {
            var result = this.playbackOCX.SetGlobalParam(xml);

            return result;
        } catch (e) {
            return 1;
        }
    }
    /**
     * 设置Token
     * 
     * @param:reqID 令牌请求ID
     * @param:token 令牌
     * @returns:成功返回0；失败返回错误码
     */
    setToken(reqID, token) {
        try {
            var result = this.playbackOCX.SetToken(reqID, token);

            return result;
        } catch (e) {
            return 1;
        }
    }

    /**
     * 录像搜索与播放
     * 
     * @param:beginTime 搜索开始时间 2013-04-18 10:00:00
     * @param:endTime 搜索结束时间 2013-04-18 10:30:00
     * @param:xml 回放搜索参数
     * @returns:操作发起成功0；失败返回错误码
     */
    setPlayback(beginTime, endTime, xml) {
        try {
            var result = this.playbackOCX.SetPlayback(beginTime, endTime, xml);
            return result;
        } catch (e) {
            return 1;
        }
    }
    /**
     * 停止回放
     * 
     * @param:index 窗口索引，-1表示停止所有窗口回放
     * @returns
     */
    stopPlayback(index) {
        try {

            this.playbackOCX.ClearPlayback(index);
            return;
        } catch (e) {
            return;
        }
    }

    /**
     * 获取当前选中窗口
     * 
     * @returns:选中窗口索引
     */
    getSelectedWindow() {
        try {
            var index = this.playbackOCX.GetSelectedWindow();
            return index;
        } catch (e) {
            return 0;
        }
    }

    /**
     * 注册回调方法(事件消息通知)
     * 
     * @param:callbackType 回调类型
     * @param:fn 回调方法
     */
    registerCallback(callbackType, fn) {
        this.callback[callbackType] = fn;
    }

    /**
     * 下载接口
     * @param:bOpen 1代表打开 0代表关闭
     * @returns:操作发起成功0；失败返回错误码
     */
    StartDownLoad(beginTime, endTime, filePath, xml) {
        try {
            var result = this.playbackOCX.StartDownLoad(beginTime, endTime, filePath, xml);
            return result;
        } catch (e) {
            return 1;
        }
    }
    /**
     * 停止下载
     * @param:bOpen 1代表打开 0代表关闭
     * @returns:操作发起成功0；失败返回错误码
     */
    StopDownload(handle) {
        try {
            var result = this.playbackOCX.StopDownload(handle);
            return result;
        } catch (e) {
            return 1;
        }
    }
    /**
     * 获取解码时间（4.0.1）新增
     * 
     * @param:windIndex 预览窗口索引
     * @returns:成功返回时间；其他-失败
     */
    getDecodeTime(windIndex) {
        try {
            var result = this.playbackOCX.GetDecodeTime(windIndex);
            return result;
        } catch (e) {
            // TODO: handle exception
            return "获取解码时间失败";
        }
    }
    /**
     * 设置窗口布局（4.2）
     * 
     * @param:layoutType 布局格式枚举见示例
     * @returns:成功返回0；失败返回错误码
     */
    SetFitLayout(layoutType) {
        try {
            var result = this.playbackOCX.SetFitLayout(layoutType);
            return result;
        } catch (e) {
            return 1;
        }
    }
    /**
     * 回放事件消息通知
     * 
     * @param:event_type 事件类型
     * @param:wnd_index 窗口索引
     * @param:lparam 事件携带的整形参数
     * @param:sparam 事件携带的字符参数
     */
     OnFireNotify(event_type, wnd_index, lparam, sparam) {
        var RealTimePlayOcx = document.getElementById("PlayBackOCX");
        switch (event_type) {	
            case 0x02000011: // 窗口选中改变
                // alert("窗口选中改变");
                break;
            case 0x02000020: // 回放开始
                // alert("回放开始");
                break;
            case 0x02000021: // 回放停止
                // alert("回放停止");
                break;
            case 0x02000006: // Token请求，在该事件中调用回放控件的SetToken接口
            console.log(this)
                this.tokenCallEvent(parseInt(lparam));
                break;
            case 0x8000001:
                break;
            default:
        }
    }
    Notify(event_type, wnd_index, lparam, sparam) {
        switch (event_type) {
            case PLAYBACK_EVENT_TOKEN:
                if (callbackOcxObj.callback[event_type]) {
                    callbackOcxObj.callback[event_type].call(this, lparam);
                }
                break;
            case PLAYBACK_EVENT_WINDOW:
                if (callbackOcxObj.callback[event_type]) {
                    callbackOcxObj.callback[event_type].call(this, wnd_index);
                }
                break;
            case PLAYBACK_EVENT_START:
                if (callbackOcxObj.callback[event_type]) {
                    callbackOcxObj.callback[event_type].call(this, wnd_index);
                }
                break;
            case PLAYBACK_EVENT_STOP:
                if (callbackOcxObj.callback[event_type]) {
                    callbackOcxObj.callback[event_type].call(this, wnd_index);
                }
                break;
            case EVENT_DOWNLOAD:
                if (callbackOcxObj.callback[event_type]) {
                    callbackOcxObj.callback[event_type].call(this, lparam, sparam);
                }
                break;
            default:
                //alert("视频回放通知消息类型错误！");
        }
    }
    /**
     * 判断浏览器类型
     * 
     * @returns:0-IE8~IE10,1-Chrome,2-IE11
     */
    isBrowserIE() {
        var appName = navigator.userAgent;
        if (appName.indexOf('MSIE') > -1) {
            return 0;
        } else if (appName.indexOf('Chrome') > -1) {
            return 1;
        } else if (!!window.ActiveXObject || "ActiveXObject" in window) {
            // 判断是否是IE11
            return 2;
        }
        return 0;
    }
}


export default PlaybackOCX